﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using PBOffice.Models;

namespace PBOffice.DAL
{
    public class RoleRightService
    {
        public List<RoleRight> GetRoleRightByRoleId(int id)
        {
            List<RoleRight> list = new List<RoleRight>();
            string sql = string.Format("select * from RoleRight where RoleId={0}", id);
            using (SqlDataReader dr = SqlHelp.ExecuteReader(SqlHelp.connectionstring, CommandType.Text, sql))
            {
                while (dr.Read())
                {
                    RoleRight right = new RoleRight();
                    right.RoleRightId = (int)dr["RoleRightId"];
                    right.RoleId = (int)dr["RoleId"];
                    right.NodeId = (int)dr["NodeId"];
                    list.Add(right);
                }
            }
            return list;
        }
        ////public void Update(RoleRight role)
        ////{
        ////    string sql = "update [RoleRight] set RoleId=@RoleId,NodeId=@NodeId where RoleRightId=@RoleRightId";
        ////    SqlParameter[] spa = {new SqlParameter("@RoleRightId",role.RoleRightId),
        ////                       new SqlParameter("@RoleId",role.RoleId),
        ////                       new SqlParameter("@NodeId",role.NodeId)
        ////                       };
        ////    SqlHelp.ExecuteNonQuery(SqlHelp.connectionstring, CommandType.Text, sql, spa);
        ////}
        public void Add(RoleRight role)
        {
            string sql = "insert [RoleRight]  (RoleId,NodeId) values(@RoleId,@NodeId)";
            SqlParameter[] spa = {
                               new SqlParameter("@RoleId",role.RoleId),
                               new SqlParameter("@NodeId",role.NodeId)
                               };
            SqlHelp.ExecuteNonQuery(SqlHelp.connectionstring, CommandType.Text, sql, spa);
        }
        public void DeleteRoleRight(int roleId)
        {
            string sql = string.Format("delete RoleRight where RoleId={0}", roleId);
            SqlHelp.ExecuteNonQuery(SqlHelp.connectionstring, CommandType.Text, sql);
        }
    }
}
